#状态机步骤
#1.init
#2.初始化event/mouse函数
#3.定义状态切换回调函数
class_name CardStateMachine

extends Node

@export var initial_state: CardState

var current_state: CardState
var states := {}

#初始化状态机状态(分两步)
#1.链接状态机状态转换信号与回调函数，
#2.初始化状态机初始状态
func init(card: CardUI) -> void:
	for child in get_children():
		states[child.state] = child
		child.transition_requested.connect(_on_transition_requested)
		child.card_ui = card
	if initial_state:
		initial_state.enter()
		current_state = initial_state
		
func on_input(event: InputEvent) -> void:
	if current_state:
		current_state.on_input(event)

func on_gui_input(event: InputEvent) -> void:
	if current_state:
		current_state.on_gui_input(event)

#疑问 这里怎么没有event: InputEvent
func on_mouse_entered() -> void:
	if current_state:
		current_state.on_mouse_entered()
		
func on_mouse_exited() -> void:
	if current_state:
		current_state.on_mouse_exited()

#transition_requested触发的回调函数
func _on_transition_requested(from: CardState, to: CardState.State) -> void:
	if from != current_state:
		return
	var new_state: CardState = states[to]
	if not new_state:
		return
	if current_state:
		current_state.exit()
	new_state.enter()
	current_state = new_state
	print("触发状态切换")
	

	


